package com.caineng.zhaobiao.zbapi.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONObject;
import com.caineng.zhaobiao.zbapi.dao.CaiCommentDao;
import com.caineng.zhaobiao.zbapi.domain.CaiBaseUser;
import com.caineng.zhaobiao.zbapi.domain.CaiBaseUserComment;
import com.caineng.zhaobiao.zbapi.utils.blackWordFilter.WordFilterUtils;
import com.caineng.zhaobiao.zbapi.utils.enums.USER_ACTION;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.LookupOperation;
import org.springframework.data.mongodb.core.aggregation.MatchOperation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 评论表-用户操作记录表
 * @author liandyao
 * @date 2022/7/29 13:58
 */
@Slf4j
@Service
public class CaiCommentService {

    /**
     * 状态有效为1
     */
    public static final String STATUS_YES = "1";
    public static final String STATUS_NO = "0";

    @Autowired
    UserService userService ;

    @Autowired
    CaiCommentDao commentDao;

    @Autowired
    MongoTemplate mongoTemplate;

    /**
     * 评论信息
     * @param busId
     * @param content
     * @param userId
     * @return
     */
    public CaiBaseUserComment addComment(String busId,String content,String userId){
        CaiBaseUserComment comment = new CaiBaseUserComment();
        comment.setBusId(busId);
        comment.setType(USER_ACTION.comment.name());
        comment.setStatus(STATUS_YES);
        comment.setCreateTime(DateUtil.now());
        comment.setUserId(userId);
        comment.setContent(WordFilterUtils.regexWordAndMobile(content));//评论内容,敏感词屏蔽
        //查询人员姓名
        CaiBaseUser userPo = userService.findUserById(userId);
        comment.setUserName(userPo.getShowName());

        return commentDao.insert(comment);
    }


    /**
     * 加入用户操作记录,不包括评论
     * @param busId
     * @param type
     * @param userId
     */
    public int addUserAction(String busId,String type,String userId){

        // 先查询一次,不能一直收藏 点赞
        CaiBaseUserComment comment2 = new CaiBaseUserComment();
        comment2.setUserId(userId);
        comment2.setType(type);
        comment2.setBusId(busId);
        List<CaiBaseUserComment> list = commentDao.findAll(Example.of(comment2));
        //如果已经点赞或者手册
        if(list.size()>0){
            //不再进行收藏和点赞
            return 10; //返回给前端
        }

        //新增点赞和收藏
        CaiBaseUserComment comment = new CaiBaseUserComment();
        comment.setBusId(busId);
        comment.setType(type);
        comment.setStatus(STATUS_YES);
        comment.setCreateTime(DateUtil.now());
        comment.setUserId(userId);
        //查询人员姓名
        CaiBaseUser userPo = userService.findUserById(userId);
        comment.setUserName(userPo.getShowName());
        commentDao.insert(comment);
        return 1; //操作成功

    }

    /**
     * 多对一查询
     * @return
     */
    public List<JSONObject> lookupComment(String userId,String type){
        // 1、添加_id字段类型转换
        //AddFieldsOperation addFieldsOperation = AddFieldsOperation.addField("busId").withValue(ConvertOperators.ToObjectId.toObjectId("$busId")).build();
        //以评论表为第一视角查询招标信息表,多表关联查询
        LookupOperation op = LookupOperation.newLookup().from("cai_zhaobiao").localField("busId").foreignField("id")
                .as("zhaobiao");
        //条件查询
        Criteria criteria = new Criteria();
        //用户ID
        criteria.and("userId").is(userId);
        //类型
        criteria.and("type").is(type) ;
        //将筛选条件放入管道中
        MatchOperation match = Aggregation.match(criteria);
        Aggregation agg = Aggregation.newAggregation(op,match);
        AggregationResults<JSONObject> commentAggregation = mongoTemplate.aggregate(agg, "cai_base_user_comment", JSONObject.class);
        List<JSONObject> list =  commentAggregation.getMappedResults() ;

        return list ;
    }


    /**
     * 删除评论等内容,根据用户id
     * @param userId
     * @param zbId
     */
    public void deleteZbIdByUserId(String userId, String zbId) {
        CaiBaseUserComment param = new CaiBaseUserComment();
        param.setBusId(zbId);
        param.setUserId(userId);
        List<CaiBaseUserComment> all = commentDao.findAll(Example.of(param));
        log.info(all.size()+"===================");
        commentDao.deleteAll(all);
    }

    /**
     * 删除评论等内容,根据用户id
     * @param userId
     * @param zbId
     */
    public void deleteZbIdByUserId(String userId, String zbId,String type) {
        CaiBaseUserComment param = new CaiBaseUserComment();
        param.setBusId(zbId);
        param.setUserId(userId);
        param.setType(type);
        List<CaiBaseUserComment> all = commentDao.findAll(Example.of(param));
        log.info(all.size()+"===================");
        commentDao.deleteAll(all);
    }
}
